<?php

namespace app\adminapi\controller\v1\marketing;

use app\adminapi\controller\AuthController;
use app\adminapi\export\StoreCouponExport;
use app\services\activity\coupon\StoreCouponServices;
use think\facade\App;

class StoreCoupon extends AuthController
{
    public function __construct(App $app, StoreCouponServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }


    public function index()
    {
        $where = $this->request->getMore([
            ['title', ''],
            ['status', -1],
            ['time_status', -1],
            ['limit', 15]
        ]);
        $list = $this->services->getList($where);

        return app('json')->success($list);
    }

    public function export()
    {
        $where = $this->request->getMore([
            ['title', ''],
            ['status', -1],
            ['time_status', -1],
            ['limit', 15]
        ]);

        return app()->make(StoreCouponExport::class)->export($where);

    }


    public function save($id)
    {
        $data = $this->request->postMore([
            ['title', ''], // 标题
            ['total_count', 0], // 总数
            ['is_use_all_product', -1], // 是否适应所有商品
            ['has_use_limit', -1], // 是否存在门槛
            ['use_min_price', 0], // 允许使用的最小金额
            ['price', 0], // 优惠劵价格
            ['start_time', -1], // 优惠劵生效开始时间
            ['end_time', -1], // 优惠劵失效结束时间
            ['use_day', -1], // 领取后多少天可以使用
            ['available_type', -1], // 有效期类型(1. 时间范围, 2. 天数)
            ['has_get_limit', -1], // 领取限制(1. 有限制, 0. 不存在限制)
            ['get_max_count', 0], // 限制领取次数
            ['products', []], // 适用商品
        ]);

        $this->services->save($id, $data);
        return app('json')->success(100001);
    }

    public function detail($id)
    {
        $coupon = $this->services->detail($id);
        return app('json')->success($coupon);
    }

    /**
     * 设置批量商品上架
     * @return mixed
     */
    public function coupon_show()
    {
        [$ids] = $this->request->postMore([
            ['ids', []]
        ], true);
        $this->services->setShow($ids, 1);
        return app('json')->success(100014);
    }

    /**
     * 设置批量商品下架
     * @return mixed
     */
    public function coupon_unshow()
    {
        [$ids] = $this->request->postMore([
            ['ids', []]
        ], true);
        $this->services->setShow($ids, 0);
        return app('json')->success(100014);
    }

    public function delete($id)
    {
        if (!$id) return app('json')->fail(100100);
        $this->services->update($id, ['is_del' => 1]);
        return app('json')->success(100002);
    }

}